package com.heima.behavior.service.impl;
import com.alibaba.fastjson.JSON;
import com.heima.common.constants.article.HotArticleConstants;
import com.heima.model.mess.app.UpdateArticleMess.UpdateArticleType;
import java.util.Date;
import java.util.UUID;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.behavior.mapper.ApLikesBehaviorMapper;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.behavior.service.ApLikesBehaviorService;
import com.heima.model.behavior.dtos.LikesBehaviorDto;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.mess.app.UpdateArticleMess;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.threadlocal.AppThreadLocalUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @作者 itcast
 * @创建日期 2021/2/4 10:25
 **/

@Service
@Slf4j
@Transactional
public class ApLikesBehaviorServiceImpl extends ServiceImpl<ApLikesBehaviorMapper, ApLikesBehavior> implements ApLikesBehaviorService {
    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;

    @Autowired
    KafkaTemplate<String,String> kafkaTemplate;

    @Override
    public ResponseResult like(LikesBehaviorDto dto) {
        log.info("like 调用，参数==>{}",dto);
        // 1. 检查参数
        if(dto ==null || dto.getArticleId() == null ){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        if(dto.getOperation() == null || (dto.getOperation()!=0 && dto.getOperation()!=1)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 2. 根据当前登录用户ID 或 设备ID 查询行为实体数据
        ApUser user = AppThreadLocalUtils.getUser();
        Integer userId = user == null?null:user.getId();
        ApBehaviorEntry behaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(userId, dto.getEquipmentId());
        if(behaviorEntry == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"行为实体数据不存在");
        }
        // 3. 根据行为实体及文章ID查询存在点赞行为
        ApLikesBehavior apLikesBehavior = getOne(
                Wrappers.<ApLikesBehavior>lambdaQuery()
                        .eq(ApLikesBehavior::getEntryId, behaviorEntry.getId())
                        .eq(ApLikesBehavior::getArticleId, dto.getArticleId())
                        .eq(ApLikesBehavior::getType, ApLikesBehavior.Type.ARTICLE.getCode())
        );
        //     不存在添加
        if(apLikesBehavior== null){
            apLikesBehavior = new ApLikesBehavior();
            apLikesBehavior.setEntryId(behaviorEntry.getId());
            apLikesBehavior.setArticleId(dto.getArticleId());
            apLikesBehavior.setType(dto.getType());
            apLikesBehavior.setOperation(dto.getOperation());
            apLikesBehavior.setCreatedTime(new Date());
            save(apLikesBehavior);
        }else {
            //     存在修改
            apLikesBehavior.setOperation(dto.getOperation());
            updateById(apLikesBehavior);
        }

        if(dto.getOperation() == 0){
            UpdateArticleMess updateArticleMess = new UpdateArticleMess();
            updateArticleMess.setType(UpdateArticleType.LIKES);
            updateArticleMess.setArticleId(dto.getArticleId());
            updateArticleMess.setAdd(1);
            kafkaTemplate.send(HotArticleConstants.HOT_ARTICLE_SCORE_TOPIC,
                    UUID.randomUUID().toString(),
                    JSON.toJSONString(updateArticleMess)
                    );
        }
        return ResponseResult.okResult();
    }
}
